package cc.lj.internal.merchant.application.service;

import cc.lj.internal.core.exception.ServiceException;
import cc.lj.internal.merchant.domain.brand.model.vo.MerchantBrandDO;
import cc.lj.internal.merchant.domain.brand.model.vo.MerchantBrandRelationDO;
import cc.lj.internal.merchant.domain.brand.service.IMerchantBrandDomainService;
import cc.lj.internal.merchant.domain.brand.service.IMerchantBrandRelationDomainService;
import cc.lj.internal.merchant.infrastructure.exception.MerchantExceptionEnum;
import cc.lj.internal.merchant.interfaces.dto.MerchantBrandDTO;
import cc.lj.internal.merchant.interfaces.dto.convert.MerchantBrandDTOConvertDO;
import cc.lj.internal.merchant.interfaces.dto.query.MerchantBrandOneQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 商户品牌 app service
 * @author liangxifeng
 * @date 2021-12-25
 */
@Slf4j
@Service(value = "merchantBrandAppService")
public class MerchantBrandAppService {
    //品牌领域
    @Resource(name = "merchantBrandDomainServiceImpl")
    private IMerchantBrandDomainService brandDomainService;
    //商户品牌关系领域
    @Resource(name = "merchantBrandRelationDomainServiceImpl")
    private IMerchantBrandRelationDomainService relationDomainService;

    /**
     * 通过品牌id查询该商户的已挂接品牌
     * @return
     */
    public List<MerchantBrandDTO> getByMerchantId(Integer merchantId) {
        //查询该商户已挂接的品牌关系
        List<MerchantBrandRelationDO> relationListDO = relationDomainService.getByMerchantId(merchantId);
        return relationListDO.stream().map(x->{
            //通过主键查询品牌信息
            MerchantBrandDO brandDO = brandDomainService.getById(x.getMbBrand());
            MerchantBrandDTO brandDTO = MerchantBrandDTOConvertDO.INSTANCE.convert(brandDO);
            return brandDTO;
        }).collect(Collectors.toList());
    }


    /**
     * 查询品牌单条数据
     * @param brandOneQuery
     * @return
     */
    public MerchantBrandDTO getOne(MerchantBrandOneQuery brandOneQuery) {
        //如果参数无商户主键，则通过品牌主键查询品牌表单条数据
        if(brandOneQuery.getMbMerchant() == null) {
            //throw new ServiceException(MerchantExceptionEnum.MERCHAND_BRAND_GETONE_IS_NULL);
            return MerchantBrandDTOConvertDO.INSTANCE.convert( brandDomainService.getById(brandOneQuery.getBrandId()) );
        }else { //如果有商户主键，则查询该商户已绑定品牌的单条数据
            //商户主键在 brandQuery实体中没有使用注解 验证参数，所以这里需要特殊处理一下
            Integer merchantId = Optional.of(brandOneQuery.getMbMerchant())
                    .filter( x -> x > 0)
                    .orElseThrow(() -> new ServiceException(MerchantExceptionEnum.MERCHANT_ID_MUST_GT_ZERO));

            MerchantBrandRelationDO relationDO = new MerchantBrandRelationDO();
            relationDO.setMbBrand(brandOneQuery.getBrandId());
            relationDO.setMbMerchant(merchantId);
            List<MerchantBrandRelationDO> relationDOS = relationDomainService.getList(relationDO);
            if(relationDOS.size() ==0) {
                throw new ServiceException(MerchantExceptionEnum.MERCHANT_BRAND_RELATION_NOT_ONE);
            }
            MerchantBrandRelationDO relationDO1 = relationDOS.get(0);
            //通过主键查询品牌信息
            MerchantBrandDO brandDO = brandDomainService.getById(relationDO1.getMbBrand());
            return MerchantBrandDTOConvertDO.INSTANCE.convert(brandDO);
        }

    }
}
